package hz.mobileproject.pda.ui.activity.work;

import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.google.zxing.activity.CaptureActivity;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.broadcast.HandleBroadcast;
import hz.mobileproject.pda.broadcast.IShowBarcodeStr;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.ui.adapter.AssetsTakingAdapter;
import hz.mobileproject.pda.ui.view.LoadingDialog;
import hz.mobileproject.pda.utils.CommonUtil;

/**
 * Created by hasee on 2017/11/16.
 * 资产盘点
 */
public class AssetsTakingActivity extends AppCompatActivity implements IShowBarcodeStr, View.OnClickListener {

    private ListView mListView;
    private Button mBtInput;
    private LinearLayout mLlInput;
    private ImageButton mIbShowInput;
    private EditText mEtInput;
    private AssetsTakingAdapter mAdapter;
    private List<AssetsTakingBean> mList = new ArrayList<AssetsTakingBean>();
    private LoadingDialog mLoadingDialog;
    private HandleBroadcast mHandleBroadcast;
    private SoundPool mSoundPool;
    private int mSoundId;
    private static final int SCAN_PLOCE_REQUEST_CODE = 0x111;
    private static final int SCAN_THING_REQUEST_CODE = 0x222;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assets_taking);
        initView();
        initData();
        initThings();
    }

    private void initThings() {
        mBtInput.setOnClickListener(this);
        mIbShowInput.setOnClickListener(this);
    }

    private void initData() {
        mAdapter = new AssetsTakingAdapter(this, mList);
        mListView.setAdapter(mAdapter);

        openScan(SCAN_PLOCE_REQUEST_CODE);
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.lv_assets_taking);
        mBtInput = (Button) findViewById(R.id.bt_assets_taking_input);
        mIbShowInput = (ImageButton) findViewById(R.id.ib_assets_taking_show_input);
        mLlInput = (LinearLayout) findViewById(R.id.ll_assets_taking_input);
        mEtInput = (EditText) findViewById(R.id.et_assets_taking_input);
        mLlInput.setVisibility(View.GONE);
    }

    /**
     * 打开扫描
     */
    private void openScan(int requestCode) {
        mLoadingDialog = new LoadingDialog(this);
        if (Constance.isNeiMeng) {  //如果是演示版本则打开相机扫描
            //打开二维码扫描界面
            if (CommonUtil.isCameraCanUse()) {
                Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, requestCode);
            } else {
                Toast.makeText(this, "请打开此应用的摄像头权限！", Toast.LENGTH_SHORT).show();
            }
        } else {
            mHandleBroadcast = new HandleBroadcast(this, this);
            mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
            mSoundId = mSoundPool.load("/etc/Scan_new.ogg", 1);
            mLoadingDialog.setMessage("请扫描民警二维码").show();
        }
    }

    @Override
    public void updateText(String msg) {
        mSoundPool.play(mSoundId, 1, 1, 0, 0, 1);
        mLoadingDialog.dismiss();
        //TODO 修改测试数据
        for (int i=1;i<=5;i++){
            AssetsTakingBean atb = new AssetsTakingBean("办公桌"+i,"1000"+i,"固定资产",AssetsTakingBean.STATE_UNFINISH);
            mList.add(atb);
        }
        for (int i=1;i<=5;i++){
            AssetsTakingBean atb = new AssetsTakingBean("电脑"+i,"2000"+i,"一般物资",AssetsTakingBean.STATE_UNFINISH);
            mList.add(atb);
        }
        for (int i=1;i<=5;i++){
            AssetsTakingBean atb = new AssetsTakingBean("KAR 98K"+i,"3000"+i,"警用装备",AssetsTakingBean.STATE_UNFINISH);
            mList.add(atb);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (SCAN_PLOCE_REQUEST_CODE == requestCode && resultCode == CaptureActivity.RESULT_CODE_QR_SCAN) {
            //TODO 修改测试数据
            for (int i=1;i<=5;i++){
                AssetsTakingBean atb = new AssetsTakingBean("办公桌"+i,"1000"+i,"固定资产",AssetsTakingBean.STATE_UNFINISH);
                mList.add(atb);
            }
            for (int i=1;i<=5;i++){
                AssetsTakingBean atb = new AssetsTakingBean("电脑"+i,"2000"+i,"一般物资",AssetsTakingBean.STATE_UNFINISH);
                mList.add(atb);
            }
            for (int i=1;i<=5;i++){
                AssetsTakingBean atb = new AssetsTakingBean("KAR 98K"+i,"3000"+i,"警用装备",AssetsTakingBean.STATE_UNFINISH);
                mList.add(atb);
            }
            mAdapter.notifyDataSetChanged();
        }
        if (SCAN_THING_REQUEST_CODE == requestCode && resultCode == CaptureActivity.RESULT_CODE_QR_SCAN) {

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_assets_taking_show_input:
                if(mLlInput.getVisibility() == View.VISIBLE){
                    mLlInput.setVisibility(View.GONE);
                }else{
                    mLlInput.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.bt_assets_taking_input:
                String id = mEtInput.getText().toString();
                int searchIndex = -1;
                for (int i=mList.size()-1; i>=0;i--){
                    if (id.equals(mList.get(i).getCode())){
                        searchIndex = i;
                        break;
                    }
                }
                final int finalI = searchIndex;
                if(finalI<0){
                    ToastUtils.showLong("没有该编码的物品");
                    return;
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(AssetsTakingActivity.this);
                builder.setItems(new String[]{"正常","异常"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mList.get(finalI).setFinishType(which);
                        mList.get(finalI).setState(AssetsTakingBean.STATE_FINISH);
                        mAdapter.notifyDataSetChanged();
                    }
                });
                builder.create().show();
                break;
        }
    }

    public class AssetsTakingBean{
        public AssetsTakingBean(String name, String code, String checkType, int state) {
            this.name = name;
            this.code = code;
            this.checkType = checkType;
            this.state = state;
        }

        private String name;
        private String code;
        private String checkType;
        public static final int STATE_FINISH = 1;
        public static final int STATE_UNFINISH = 0;
        private int state;
        public static final int FINISHTYPE_OK = 0;
        public static final int FINISHTYPE_ERR = 1;
        private int finishType;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getCheckType() {
            return checkType;
        }

        public void setCheckType(String checkType) {
            this.checkType = checkType;
        }

        public int getState() {
            return state;
        }

        public void setState(int state) {
            this.state = state;
        }

        public int getFinishType() {
            return finishType;
        }

        public void setFinishType(int finishType) {
            this.finishType = finishType;
        }
    }
}
